package system

import (
	"server/common/dto/response"
	"server/core"
	serviceRequest "server/service/dto/request"
	"server/service/v1/system"

	"github.com/gin-gonic/gin"
)

type RoleHandler struct {
	RoleService system.RoleService
}

func NewRoleHandlerApp(service *system.RoleService) *RoleHandler {
	return &RoleHandler{RoleService: *service}
}

// @Summary 创建角色信息
// @Tags 角色管理
// @Accept json
// @Produce json
// @Param data body request.CreateRoleRequest true "创建角色信息请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v1/role/create [post]
func (rh *RoleHandler) CreateRoleInfoHandler(ctx *gin.Context) {
	var createRequest serviceRequest.CreateRoleRequest
	if !core.ValidateParams(ctx, &createRequest) {
		response.Failed(ctx)
		return
	}
	if err := rh.RoleService.CreateRoleInfoService(&createRequest); err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 删除角色信息
// @Tags 角色管理
// @Accept json
// @Produce json
// @Param id path string true "删除角色信息请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v1/role/delete/{id} [delete]
func (rh *RoleHandler) DeleteRoleInfoHandler(ctx *gin.Context) {
	id := ctx.Param("id")
	if !core.ValidateParams(ctx, &id) {
		return
	}
	responseData, err := rh.RoleService.DeleteRoleInfoService(id)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 修改角色信息
// @Tags 角色管理
// @Accept json
// @Produce json
// @Param data body request.ModifyRoleRequest true "修改角色信息请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v1/role/modify [put]
func (rh *RoleHandler) ModifyRoleInfoHandler(ctx *gin.Context) {
	var modifyRequest serviceRequest.ModifyRoleRequest
	if !core.ValidateParams(ctx, &modifyRequest) {
		return
	}
	responseData, err := rh.RoleService.ModifyRoleInfoSerice(&modifyRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 获取角色详情
// @Tags 角色管理
// @Accept json
// @Produce json
// @Param id path string true "获取角色详情请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v1/role/info/{id} [get]
func (rh *RoleHandler) GetRoleInfoHandler(ctx *gin.Context) {
	id := ctx.Param("id")
	if !core.ValidateParams(ctx, &id) {
		return
	}
	responseData, err := rh.RoleService.GetRoleInfoService(id)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 获取角色列表
// @Tags 角色管理
// @Accept json
// @Produce json
// @Param name query string true "角色名称"
// @Param code query string true "角色代码"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v1/role/list [get]
func (rh *RoleHandler) GetRoleListHandler(ctx *gin.Context) {
	var queryRequst serviceRequest.RoleQueryRequest
	if !core.ValidateParams(ctx, &queryRequst) {
		return
	}
	responseData, err := rh.RoleService.GetRoleListService(&queryRequst)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 获取角色分页列表
// @Tags 角色管理
// @Accept json
// @Produce json
// @Param page query int true "页码"
// @Param size query int true "每页大小"
// @Param name query string true "角色名称"
// @Param code query string true "角色代码"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v1/role/page [get]
func (rh *RoleHandler) GetRolePageHandler(ctx *gin.Context) {
	var queryRequst serviceRequest.RoleQueryRequest
	if !core.ValidateParams(ctx, &queryRequst) {
		return
	}
	responseData, err := rh.RoleService.GetRolePageService(&queryRequst)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}
